package com.example.springboot.controller.user;

import com.example.springboot.constant.ResConstansts;
import com.example.springboot.entity.Collect;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.dto.CollectDto;
import com.example.springboot.service.CollectService;
import com.example.springboot.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user_collects")
public class CollectController {
    @Autowired
    private CollectService collectService;
    private JwtUtil jwtUtil;
    @PostMapping
    public Res<Void> follow(@RequestHeader(value = "Authorization", required = false) String token, @RequestBody CollectDto collectDto) {
        if (token == null)
            return new Res<Void>().code(401).msg("未登录");
        try {
            String reqToken = token.substring(7);// 去掉 "Bearer " 前缀
            Integer collectId=jwtUtil.getUserIdByToken(reqToken);
            collectService.collect(collectDto,collectId);
            return new Res<Void>().code(200).msg("操作成功");
        } catch (Exception e) {
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
    @GetMapping("/{collectedId}")
    public Res<Integer> getIsCollect(@RequestHeader(value = "Authorization", required = false) String token ,@PathVariable Integer collectedId) {
        if (token == null)
            return new Res<Integer>().code(401).msg("未登录").data(0);
        try {
            String reqToken = token.substring(7);// 去掉 "Bearer " 前缀
            Integer collectId=jwtUtil.getUserIdByToken(reqToken);
            Collect collect=collectService.getIsCollect(collectedId,collectId);
            if(collect==null || collect.getIsCollect()==0){
                return new Res<Integer>().code(200).msg("操作成功").data(0);
            }else{
                return new Res<Integer>().code(200).msg("操作成功").data(1);
            }
        } catch (Exception e) {
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
}
